using SDK.Scripts.Utils;
using System;
using System.Net;
using System.Runtime.InteropServices;

namespace SDK.Scripts.Network
{
	public class BaseMsg
	{
		private static readonly object locker = new object();

		protected void ConvertByte<T>(T con, byte[] bytes, int startIndex)
		{
			int num = Marshal.SizeOf(con.GetType());
			byte[] array = new byte[num];
			if (typeof(T) == typeof(short) || typeof(T) == typeof(int))
			{
				int num2 = 0;
				num2 = ((!(typeof(T) == typeof(short))) ? Convert.ToInt32(con) : Convert.ToInt16(con));
				num2 = IPAddress.HostToNetworkOrder(num2);
				array = BitConverter.GetBytes(num2);
			}
			else if (typeof(T) == typeof(byte))
			{
				array[0] = Convert.ToByte(con);
			}
			Array.Copy(array, 0, bytes, startIndex, num);
		}

		protected bool SpliteHeadMsg<T>(T con, byte[] bytes, ref int startIndex, out byte[] byteResult)
		{
			lock (locker)
			{
				int num = Marshal.SizeOf(con.GetType());
				byteResult = new byte[num];
				if (startIndex + num > bytes.Length)
				{
					Debuger.LogError("SpliteHeadMsg startIndex = " + startIndex + " , length =" + num + " , bytes.Length = " + bytes.Length + ",con = " + con.ToString());
					return false;
				}
				Array.Copy(bytes, startIndex, byteResult, 0, num);
				startIndex += num;
				return true;
			}
		}

		public virtual bool encodeHead(ref byte[] bytes)
		{
			return false;
		}

		public virtual bool decodeHead(ref byte[] bytes)
		{
			return false;
		}
	}
}
